/**
* Copyright (C) 2021 All rights reserved.
*
* @file main.h
* @author Wang Haibin
* @email wanghaibin@qiyi.com
* @date Wed Aug  7 20:53:51 CST 2024
* @brief  ip字典树，根据ip段内容获取对应的ip
*/

#include <iostream>

#include "dp_trie_ip.h"

void test_check_search(dp::dp_trie_ip& trie, const std::string& value, const std::string* result)
{
    auto r = (std::string*)trie.search(value);
    printf("%s search'%s' result is '%s=%s'\n",
        r == result ? "success" : "failed",
        value.c_str(), r ? (*r).c_str() : "NULL", result ? result->c_str() : "NULL");
}

int main()
{
    std::string PA("Policy A"), PB("Policy B"), PC("Policy C"), PD("Policy D"), PE("Policy E"), PF("Policy F");
    dp::dp_trie_ip trie;
    trie.insert("192.168.0.0/24", &PA);
    trie.insert("192.168.0.0/16", &PB);
    trie.insert("10.0.0.0/8", &PC);
    trie.insert("2001:0db8::/32", &PD);
    trie.insert("2001:0db8:85a3::/48", &PE);
    trie.insert("2001:0db8:85a3:0000:0000:8a2e:0370:7334/128", &PF);

    test_check_search(trie, "2001:0db8:85a3::8a2e:0370:7334", &PF);
    test_check_search(trie, "192.168.0.1", &PA);
    test_check_search(trie, "192.168.1.1", &PB);
    test_check_search(trie, "10.0.1.1", &PC);
    test_check_search(trie, "2001:0db8:85a3::1", &PE);
    test_check_search(trie, "2001:0db8::1", &PD);
    test_check_search(trie, "172.16.0.1", NULL);
    return 0;
}
